

// at ([['type', 'payload', 'callback']])
export const useProps = (ctx) => {
  const { dispatch, ...restOfAll } = ctx;

  return {
    at (typeOrArray, payload, callback) {
      if ( typeOrArray instanceof Array ) {
        for (const idx in typeOrArray) {
          const [ type, payload, callback ] = typeOrArray[idx];
          dispatch({
            type,
            payload,
            callback,
          })
        }
      } else {
        dispatch({
          type: typeOrArray,
          payload,
          callback,
        })
      }
      
    },
    dispatch,
    ...restOfAll
  }
}

export const formParams = (fields, prevState) => {
  let values = { };

  if (fields['date_range']) {
    // console.log(fields);
    const [start_date, end_date] = rangedate2str(fields['date_range'], 'YYYY-MM-DD HH:mm:ss');

    // console.log(start_date, end_date);

    delete fields['date_range'];

    values = {
      ...fields,
      start_date,
      end_date
    };
  } else {
    values = {
      ...fields,
    }
  }

  if (prevState) {
    return {
      ...prevState,
      ...values,
    }
  }

  return values;
}

// import React, { useState, useEffect } from 'react';
import { rangedate2str } from './moment';
import { useState } from 'react';

// 视图层大牛
export const within = function (props, type) {
  const {
    dispatch,
  } = props;
  const that = this;
  const go = () => {
    dispatch({
      type,
      payload: that,
    });
  }

  return {
    of: (fields, prevState) => {
      let values = { };

      if (fields['date_range']) {
        const [start_date, end_date] = rangedate2str(fields['date_range'], 'yyyy-MM-dd HH:mm:ss');
        delete fields['date_range'];

        values = {
          ...fields,
          start_date,
          end_date
        };
      } else {
        values = {
          ...fields,
        }
      }

      if (prevState) {
        return {
          ...prevState,
          ...values,
        }
      }

      return values;
    },
    go,
  }
}

export const useScurd = (props, type, stately) => {
  const {
    dispatch,
  } = props;
  const [searchValues, setSearchValues] = useState({});//stately;//useState(initialState);

  // if (searcher) {
  //   const { searchValues } = compoState;

  //   compoState.searcher = searcher;
  // } else {
  //   const { searchValues, searcher } = compoState;

  //   return {}
  // }
  


  return {
    // searchValues, setSearchValues, 
    setValues: (fields, prevState) => {
      let values = { };

      if (fields['data_range']) {
        const [start_date, end_date] = rangedate2str(fields['date_range'], 'yyyy-MM-dd HH:mm:ss');
        delete fields['date_range'];

        values = {
          ...fields,
          start_date,
          end_date
        };
      } else {
        values = {
          ...fields,
        }
      }

      setSearchValues({
        ...values,
      }, () => {
        fetch();
      });
    },
    fetch
  };
}